package xyz.chaobei;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class CustomServlet implements Servlet {

    private final Logger logger = LoggerFactory.getLogger(CustomServlet.class);

    private ServletConfig servletConfig;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;
        logger.info("CustomServlet,init name={}", servletConfig.getServletName());
        logger.info("CustomServlet,init class={}", servletConfig.getClass());
    }

    @Override
    public ServletConfig getServletConfig() {
        return this.servletConfig;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        logger.info("CustomServlet method={}", request.getMethod());

        PrintWriter printWriter = response.getWriter();
        printWriter.println("Hello CustomServlet");
    }

    @Override
    public String getServletInfo() {
        return "CustomServlet mrc";
    }

    @Override
    public void destroy() {
        logger.warn("CustomServlet name={}", servletConfig.getServletName());
    }

}
